package com.kzd.gtgame.main.game

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.GameViewModel
import com.kzd.gtgame.main.KEY_GAME_DETAILS_ID
import com.kzd.gtgame.main.KEY_GAME_DETAILS_TITLE
import com.kzd.gtgame.main.game.adapter.GameServiceAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_service_more.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class ServiceMoreActivity : OwnerViewModeActivity<GameViewModel>() {

    private val mGameServiceAdapter = GameServiceAdapter()

    private val mTitle: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_TITLE) ?: ""

    private val mGameId: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_ID) ?: ""

    override fun viewModel(): GameViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_service_more

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flServiceMoreBar)
    }

    override fun initView() {

        flServiceMoreBar.setTitleTextContent(mTitle)

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
            itemAnimator = null
        }.adapter = mGameServiceAdapter

        mGameServiceAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mGameServiceAdapter.getItem(position)
            if (view.id == R.id.tvGameServiceRemindStatus) {
                viewModel.gameServiceRemind(itemData.id, itemData.remind_rs, position)
            }
        }

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.gameService(mGameId, limit = 20)
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.gameService(mGameId, isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.gameService(mGameId, limit = 20)
    }

    override fun initViewMode() {
        viewModel.gameServiceLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,mGameServiceAdapter,mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?:return@observe
            if (isLoadMore) {
                mGameServiceAdapter.addData(data)
            } else {
                mGameServiceAdapter.setNewInstance(data)
            }
        })

        viewModel.serviceRemindLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data
                ?: return@observe
            val gameService = mGameServiceAdapter.data[data.position].apply {
                this.remind_rs = data.isRemind
            }
            mGameServiceAdapter.setData(data.position, gameService)
        })
    }
}